// 系统管理相关接口
import { getFileContext, doPagination, responseStatus, addOneData, setFileContext, getDataByKey, updateOneData, removeOneData } from './util'
import { managerFile as filePath } from './filePath'
import dayjs from 'dayjs'
export default [
  {
    url: '/api/system/manager/list',
    method: 'get',
    timeout: 200,
    response: ({ query }) => {
      const data = getFileContext(filePath)
      const res = doPagination(data.reverse(), query.page, query.pageSize)
      return {
        ...responseStatus(),
        data: res,
        total: data.length
      }
    }
  },
  {
    url: '/api/system/manager/add',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(filePath)
      const res = getDataByKey(data, body.name, 'name')
      if (res) {
        return {
          ...responseStatus(2),
          msg: '已存在相同名称的角色'
        }
      }
      body.createTime = dayjs().format('YYYY-MM-DD hh:mm:ss')
      addOneData(data, body)
      setFileContext(filePath, data)
      return {
        ...responseStatus()
      }
    }
  },
  {
    url: '/api/system/manager/info',
    method: 'get',
    timeout: 200,
    response: ({ query }) => {
      const data = getFileContext(filePath)
      const res = getDataByKey(data, Number(query.id))
      if (res) {
        return {
          ...responseStatus(),
          data: res
        }
      } else {
        return {
          ...responseStatus(2)
        }
      }
    }
  },
  {
    url: '/api/system/manager/update',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(filePath)
      body.updateTime = dayjs().format('YYYY-MM-DD hh:mm:ss')
      updateOneData(data, body)
      setFileContext(filePath, data)
      return {
        ...responseStatus()
      }
    }
  },
  {
    url: '/api/system/manager/remove',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(filePath)
      removeOneData(data, body.id)
      setFileContext(filePath, data)
      return {
        ...responseStatus()
      }
    }
  }
]
